﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PeoplesJukebox.Core.Interfaces;
using PeoplesJukebox.Core.MediaItems;
using System.Threading;

namespace PeoplesJukebox.Core
{
    /// <summary>
    /// Service class exposing catalogue and replay services
    /// </summary>
    public static class PeoplesJukeboxService
    {
        private static SelectedTrack CurrentTrack { get; set; }

        /// <summary>
        /// Gets the next track to be played
        /// </summary>
        /// <returns>Track</returns>
        public static Track GetNextTrack()
        {
            Track track = null;
            foreach (ITrackList trackList in Container.TrackLists)
            {
                track = trackList.GetNextTrack();
                if (track != null)
                {
                    CurrentTrack = new SelectedTrack();
                    CurrentTrack.Title = track.Name;
                    CurrentTrack.AlbumTitle = track.AlbumName;
                    CurrentTrack.ArtistName = track.ArtistName;
                    CurrentTrack.ChosenBy = trackList.ChosenByText;
                    break;
                }
            }
            return track;
        }

        /// <summary>
        /// Resets the tracklists
        /// </summary>
        public static void Reset()
        {
            foreach (ITrackList trackList in Container.TrackLists)
            {
                trackList.Load();
            }
        }

        /// <summary>
        /// Starts playback
        /// </summary>
        public static void Start()
        {
            Stop();
            Container.Player.Start();
        }

        /// <summary>
        /// Stops playback
        /// </summary>
        public static void Stop()
        {
            Container.Player.Stop();
        }

        /// <summary>
        /// Skips playback to the next track
        /// </summary>
        public static void Skip()
        {
            Container.Player.Skip();
        }

        /// <summary>
        /// Adds the selected track to the queue
        /// </summary>
        /// <param name="trackId">Track ID</param>
        /// <returns>Whether track was successfully queued</returns>
        public static bool AddSelectedTrack(int trackId)
        {
            return Container.SelectedTrackList.AddTrack(trackId);
        }

        /// <summary>
        /// Returns the currently playing track
        /// </summary>
        /// <returns></returns>
        public static SelectedTrack GetCurrentTrack()
        {
            return CurrentTrack;
        }
    }
}
